import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.TreeSet;

import controlP5.Bang;
import controlP5.Button;
import controlP5.ControlCanvas;
import controlP5.ControlEvent;
import controlP5.ControlGroup;
import controlP5.ControlP5;
import controlP5.ListBox;
import controlP5.RadioButton;
import controlP5.ScrollList;
import controlP5.Toggle;
import processing.core.PApplet;
import processing.core.PFont;
import java.sql.ResultSetMetaData;

public class Main extends PApplet {
	//static parameters
	public final static int WIDTH = 800;
	public final static int HEIGHT = 800;	
	public final static int FRAME_RATE = 30;
	
	//public variables to access Processing and ControlP5 libraries
	public ControlP5 controlP5;
	public PApplet theApplet = this;
	public CSVConn myconn;
	
	//static variables 
	public static ArrayList<MainGroup> groupList;  //holds all created groups
	public static MainGroup main;				   //group "main" to hold all objects that do not fall in another group
	public static DrawObject activeObject;		   //object selected by user, when mouse button is not down it takes null value
	public static HashMap<String, Integer> nameToId;				   //hashmap converting group names to ids
	public static HashMap<Integer, String> idToName;				   //hashmap converting group ids to names
	public static int nextId;
	public static PFont font12;
	
	
	int testCanvas_xPos = 100;
	int testCanvas_yPos = 200;
	int testCanvas_width = 300;
	int testCanvas_height = 400;
	
	ArrayList<String> cleanUpList; //MOVE LATER
	DataControlGroup dataGroup;
	
	
	//sets up window and p5 start parameters
	public void setup() {
		size(WIDTH,HEIGHT);
		smooth();
		frameRate(FRAME_RATE);
		
		controlP5 = new ControlP5(this);
		nameToId = new HashMap<String, Integer>();
		idToName = new HashMap<Integer, String>();
		nextId = 0;
		font12 = createFont("FFScala", 12);
		
		initializeGroups();
		//initializeData();
		dataGroup = new DataControlGroup(controlP5, this, 100, 200);
		
		
	}
	
	//draws objects at each iteration of the buffer
	//should not need to use at this level
	public void draw() {
		 background(0);	
		 //drawStuff();
	}
	void drawStuff() {
		rect(0,0,50,50);
	}
	
	//handles mouse press event
	//checks all open groups to see if mouse click is in their range
	//goes in order specified by groupList, then checks "main" group
	public void mousePressed() {
		for(MainGroup g: groupList) {
			if(g.handlePress(mouseX, mouseY)) {
				return;
			}
			main.handlePress(mouseX, mouseY);
		}
	}
	
	//handles mouse release event
	//calls handleRelease function in DrawObject class
	public void mouseReleased() {
		if(activeObject != null) {
			activeObject.handleRelease(mouseX, mouseY);
		}
	}
	
	
	//Initialize a couple groups for testing purposes
	void initializeGroups() {
		groupList = new ArrayList<MainGroup>();
		main = new MainGroup( "MainGroup", this, controlP5,0, 0, width, height);
		main.theGroup.setBackgroundColor(color(0,0,0,255));
				
		for(int i = 0; i< 1; i++) {
			MainGroup g = new MainGroup( "Group: "+i, this, controlP5,testCanvas_xPos + 420*(i), testCanvas_yPos+ 50*i, testCanvas_width, testCanvas_height);
			groupList.add(g);
			DrawObject c = new DrawObject(this, g.theCanvas, g.theGroup, 100, 100);
			g.theCanvas.drawQueue.add(c);
			g.theCanvas.parent.theGroup.addCanvas(c);
		}		
	}
	
	void controlEvent(ControlEvent theEvent) {
		dataGroup.controlEvent(theEvent);
	}
}
